<template>
  <div class="register-page">
    <div class="register-container">
      <div class="register-left">
        <div class="welcome-text">
          <h1>加入我们</h1>
          <p>开启您的专属体验</p>
        </div>
      </div>
      
      <div class="register-right">
        <div class="register-card">
          <div class="register-header">
            <h2>用户注册</h2>
            <p>填写信息完成账号创建</p>
          </div>
          
          <el-form 
            :model="registerForm" 
            :rules="rules" 
            ref="registerFormRef" 
            label-position="top"
            class="register-form"
          >
            <el-form-item label="姓名" prop="name">
              <el-input 
                v-model="registerForm.name" 
                placeholder="请输入真实姓名"
                prefix-icon="UserFilled"
                size="large"
              />
            </el-form-item>
            
            <el-form-item label="用户名" prop="username">
              <el-input 
                v-model="registerForm.username" 
                placeholder="4-16位字母或数字"
                prefix-icon="User"
                size="large"
              />
            </el-form-item>
            
            <el-form-item label="密码" prop="password">
              <el-input 
                type="password" 
                v-model="registerForm.password" 
                placeholder="6-18位密码，区分大小写"
                show-password
                prefix-icon="Lock"
                size="large"
              />
            </el-form-item>
            
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input 
                type="password" 
                v-model="registerForm.confirmPassword" 
                placeholder="请再次输入密码"
                show-password
                prefix-icon="Lock"
                size="large"
              />
            </el-form-item>
            
            <el-button 
              type="primary" 
              size="large" 
              round 
              @click="submitForm"
              class="register-btn"
            >
              立即注册
            </el-button>
            
            <div class="register-footer">
              <p>已有账号？<el-link type="primary" @click="$router.push('/login')">去登录</el-link></p>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import http from '@/utils/request'
import { encryptPassword } from '@/utils/crypto';


const registerFormRef = ref(null);
const registerForm = ref({
  name: '',
  username: '',
  password: '',
  confirmPassword: ''
});

const validatePassword = (rule, value, callback) => {
  if (value !== registerForm.value.password) {
    callback(new Error('两次输入的密码不一致'));
  } else {
    callback();
  }
};

const rules = {
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { min: 2, max: 10, message: '长度在2到10个字符', trigger: 'blur' }
  ],
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 4, max: 16, message: '长度在4到16个字符', trigger: 'blur' },
    { pattern: /^[a-zA-Z0-9]+$/, message: '只能包含字母和数字', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 18, message: '长度在6到18个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { validator: validatePassword, trigger: 'blur' }
  ]
};

const submitForm = () => {
  registerFormRef.value.validate((valid) => {
    if (valid) {
      ElMessage.success('注册成功！');
      // 这里可以添加实际的注册逻辑
      let data = {
        username: registerForm.value.username,
        password: encryptPassword(registerForm.value.password),
        name: registerForm.value.name
      }
      console.log(data)
      return
      http.post('/system_user/register', data).then(res => {
        console.log(res);
      }).catch(err => {
        console.log(err);
      });
    } else {
      return false;
    }
  });
};
</script>

<style scoped>
.register-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
}

.register-container {
  display: flex;
  width: 900px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.register-left {
  flex: 1;
  padding: 60px;
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.register-left .welcome-text h1 {
  font-size: 32px;
  margin-bottom: 16px;
  font-weight: 600;
}

.register-left .welcome-text p {
  font-size: 16px;
  opacity: 0.9;
}

.register-right {
  flex: 1;
  padding: 60px;
  display: flex;
  align-items: center;
}

.register-card {
  width: 100%;
}

.register-header {
  margin-bottom: 40px;
  text-align: center;
}

.register-header h2 {
  font-size: 28px;
  color: #303133;
  margin-bottom: 8px;
  font-weight: 600;
}

.register-header p {
  color: #909399;
  font-size: 14px;
}

.register-form .el-form-item__label {
  font-weight: 500;
  color: #606266;
  margin-bottom: 8px;
}

.register-btn {
  width: 100%;
  margin-top: 10px;
  font-size: 16px;
  letter-spacing: 1px;
}

.register-footer {
  margin-top: 24px;
  text-align: center;
  color: #909399;
  font-size: 14px;
}

.register-footer p {
  margin: 0;
}

@media (max-width: 768px) {
  .register-container {
    flex-direction: column;
    width: 100%;
  }
  
  .register-left {
    padding: 40px 20px;
  }
  
  .register-right {
    padding: 40px 20px;
  }
}
</style>
